package com.y17376065_yzh_FinalTest.rdtt.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.y17376065_yzh_FinalTest.rdtt.R;

public class LogActivity extends Activity {

    //控件申明
    Button btn_login;
    EditText et_password, et_username;
    //用户名
    String username;
    //密码
    String password;
    private DB dbHelper;
    private TextView tv_regist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log);
        dbHelper = new DB(this, "1376065.db", null, 2);
        //初始化控件
        et_password = findViewById(R.id.et_password);
        et_username = findViewById(R.id.et_username);
        btn_login = findViewById(R.id.btn_login);
        tv_regist = findViewById(R.id.tv_regist);
        //登陆按钮点击事件

        new Thread(new Runnable(){
            @Override
            public void run() {
                AccessToServer login = new AccessToServer("http://172.20.10.13:8080/AndroidServers/LoginServlet");
                //LoginToServer login = new LoginToServer();
                String result = login.doPost(new String[]{"edit_account","edit_password"},new String[]{edit_account.getText().toString(),edit_password.getText().toString()});
                Message msg = new Message();
                msg.obj = result;
                myHandler.sendMessage(msg);
            }
        }).start();

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                username = et_username.getText().toString();
                password = et_password.getText().toString();
                if (login(username, password)) {
                    Toast.makeText(LogActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(LogActivity.this,MainActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(LogActivity.this, "登陆失败,请检查账号密码是否正确", Toast.LENGTH_SHORT).show();
                }
            }
        });
        tv_regist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LogActivity.this, RegisActivity.class);
                startActivity(intent);
            }
        });
    }

    //验证登录
    public boolean login(String username, String password) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String sql = "select * from username where name=? and password=?";
        Cursor cursor = db.rawQuery(sql, new String[]{username, password});
        if (cursor.moveToFirst()) {
            cursor.close();
            return true;
        }
        return false;
    }
}
    /**
     * 读取SharedPreferences存储的键值对
     * */
    public void readUsersInfo(){
        SharedPreferences sharedPreferences = getSharedPreferences("UsersInfo",MODE_PRIVATE);
        account = sharedPreferences.getString("username","");
        password = sharedPreferences.getString("password","");
    }
}